   package com.javalong.affairs.module.notifything.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.ui.TextSelectView;
import com.javalong.affairs.base.ui.TextSelectView.OnImageClick;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_grant)
public class GrantActivity extends UIHandlerActivity implements OnImageClick {
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.ui_tsview_grant)
	private TextSelectView tsView_grant;
	@ViewInject(R.id.bt_ok)
	private Button bt_ok;
	private StringBuilder granterStr = new StringBuilder();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initUI();
	}

	private void initUI() {
		topTitle.setOnLeftDrawableClick(this);
		bt_ok.setOnClickListener(this);
		tsView_grant.setOnImageClick(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.bt_ok:
			UrlUtils.getUrl("Section", "empower");
			break;
		case R.id.ui_tsview_grant:
			IntentManager.doActivityForResult(this, ReceiverActivity.class,
					RequestCode.RECEIVER);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case RequestCode.RECEIVER:
				granterStr = new StringBuilder();
				List<User> userList = (List<User>) data
						.getSerializableExtra(BundleKey.USERLIST);
				StringBuilder sb = new StringBuilder();
				for (User u : userList) {
					sb.append(u.section).append("-").append(u.userName)
							.append(";");
					granterStr.append(u.userName).append(",");
				}
				granterStr.deleteCharAt(granterStr.length() - 1);
				sb.deleteCharAt(sb.length() - 1);
				tsView_grant.setText(sb.toString());
				break;
			}
		}
	}
}
